import React from 'react';
import {List, Button} from 'antd-mobile';
import BaseComponent from "../base/BaseComponent";
import CowImg from '../assets/image/cow.png'

import {GetLeaveEnterInfoList,SaveLeave,LeaveEnterRedisDelete} from "../http/Request";

import OutSlaughterStyle from '../assets/css/OutSlaughterAdd.module.css'
import EntryAddListStyle from "../assets/css/EntryAddList.module.css";

const Item = List.Item;


export default class OutSlaughterAdd extends BaseComponent {
    // eslint-disable-next-line no-useless-constructor
    constructor(props) {
        super(props);
        this.OutSlaughterAddDetail = this.OutSlaughterAddDetail.bind(this);
        this.state = {
            totalPrice:'',
            list: []
        }
    }

    /**
     * 生命周期
     * */
    componentDidMount() {
        this.GetLeaveEnterInfoList()
    }

    //获取总金额
    onChangetotalPrice = (e) => {
        this.setState({
            totalPrice:e.target.value
        })
    }

    OutSlaughterAddDetail = (props) => {
        this.PushPage('OutSlaughterAddDetail/' + props['infoId'])
    };

    /**
     * 获取--出栏缓存列表
     * */
    GetLeaveEnterInfoList() {
        let params = {
            id: '1010'
        };
        GetLeaveEnterInfoList(params).then(res => {
            console.log(res)
            this.setState({
                list:res.body
            })
        }).catch(err => {

        })
    };


    /**
     * 删除出栏
     * */
    LeaveEnterRedisDelete(){
        console.log('删除缓存')
        let body = {
            id:'1010'
        }
        LeaveEnterRedisDelete(body).then(res => {
            this.GetLeaveEnterInfoList()
            // this.showToast()
        }).catch(err => {

        })
    }

    /**
     * 完成
     * */
    EntryAddDone(){
        let body ={
            orderType:1010,
            amountPrice:parseInt(this.state.totalPrice),
            amountNumber:parseInt(this.state.list.length),
            leaveEnterInfos:this.state.list
        };

        SaveLeave(body).then(res => {
            LeaveEnterRedisDelete(
                body = {
                    id:'1010'
                }
            ).then(res => {
                this.GetLeaveEnterInfoList()
                this.showToast()
            }).catch(err => {

            })
        }).catch(err => {

        })
    }


    list(){
        if(this.state.list == null){
            return(
                <div className={EntryAddListStyle.NoData}>
                    暂无数据
                </div>
            )
        }else{
            return(
                <List style={{padding: '0px'}}>
                    {
                        this.state.list.map((data, index) => {
                            return this.OutSlaughterListItem(data, index)
                        })
                    }
                </List>
            )
        }

    }

    /**
     * 列表
     * */
    OutSlaughterListItem = (data, index) => {
        return (
                <Item key={index} className={OutSlaughterStyle.item}>
                    <div className={OutSlaughterStyle.timeBox}>
                        编号：{data.infoId}
                    </div>
                    <div className={OutSlaughterStyle.contentBox}>
                        <div className={OutSlaughterStyle.imgBox}>
                            <img src={CowImg} className={OutSlaughterStyle.cow_img} alt=""/>
                        </div>
                        <div className={OutSlaughterStyle.InfoBox}>
                            <div>
                                日期：{data.leaveTime}
                            </div>
                            <div>
                                体重：{data.leaveWeight}kg
                            </div>
                            <div>
                                价格：{data.leavePrice}¥
                            </div>
                        </div>

                    </div>
                </Item>
        )
    };


    render() {
        return (
                <div className={'main'}>
                    <div className={OutSlaughterStyle.topPart}>

                    </div>
                    <div className={OutSlaughterStyle.contentPart}>
                        <div className={OutSlaughterStyle.middelBox}>
                            <div className={OutSlaughterStyle.inputBox}>
                                <label>总金额:</label><input onChange={this.onChangetotalPrice}
                                                          value={this.state.totalPrice}  className={OutSlaughterStyle.inputMoney} type="number"/>
                            </div>
                            <div className={OutSlaughterStyle.buttonBox}>
                                {/*<Button style={{marginRight: '10px'}} type="ghost" inline onClick={this.LeaveEnterRedisDelete.bind(this)} size="small">删除缓存</Button>*/}
                                {/*<Button type="primary" onClick={this.OutSlaughterAddDetail.bind(this)} inline size="small" style={{marginRight: '10px'}}>新增</Button>*/}
                                <Button style={{marginRight: '10px'}} type="ghost" onClick={this.EntryAddDone.bind(this)} inline size="small">完成</Button>
                            </div>
                        </div>
                        {this.list()}
                    </div>
                </div>
        )
    }
}
